﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

namespace ServiceOperater.Converters
{
    // E:\Dropbox\CodeLib-CodeMonkey\WpfLib\Converters
    public abstract class BooleanConverter<T> : IValueConverter
    {
        protected BooleanConverter(T trueValue, T falseValue)
        {
            this.TrueValue = trueValue;
            this.FalseValue = falseValue;
        }

        public T TrueValue { get; set; }

        public T FalseValue { get; set; }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Object.Equals(value, true) ? this.TrueValue : this.FalseValue;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Object.Equals(value, this.TrueValue) ? true : false;
        }
    }


    /*  Usage 用法
     
    //可以通过改变TrueValue和FalseValue属性来改变转换行为
    public sealed class BooleanToVisibilityConverter : BooleanConverter<Visibility>
    { //	缺省情况下:true--->Visible,false--->Collapsed
     
         //使用显式的静态构造函数来告诉C#编译器不要将类型标记为beforefieldinit;精确控制静态属性的实例化时机;
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static BooleanToVisibilityMultiConverter()
        {
        }

        private BooleanToVisibilityMultiConverter()
        {
        }
        private static BooleanToVisibilityConverter converter;
        public static BooleanToVisibilityConverter Instance
     {
        return converter ?? (converter = new BooleanToVisibilityConverter());
      }
        public BooleanToVisibilityConverter() : base(Visibility.Visible, Visibility.Collapsed) { }
    }
    public sealed class BooleanToStringConverter : BooleanConverter<string>
    {
        public BooleanToStringConverter(): base(null, null) { }
    }
    //is useful for displaying different colors or images based on a Boolean value
    public sealed class BooleanToBrushConverter : BooleanConverter<Brush>
    {
        public BooleanToBrushConverter(): base(null, null) { }
    }
    // is helpful when binding to the Opacity dependency property
    public sealed class BooleanToDoubleConverter : BooleanConverter<double>
    {//converts true to 1 and false to 0 by default
        public BooleanToDoubleConverter(): base(1, 0) { }
    }
    //is handy when binding IsEnabled or IsChecked to data properties that have the opposite meaning
    public sealed class NegatedBooleanConverter : BooleanConverter<bool>
    {//颠倒true,false的转换
        public NegatedBooleanConverter() : base(false, true) { }
    }
    */
}
